package au.org.htd.youslide;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.util.StringTokenizer;
import java.util.logging.Logger;

import com.google.appengine.repackaged.com.google.common.util.Base64;

public class Util {
   public static final String DIVIDER ="--------------------------------------";
   @SuppressWarnings("unused")
   private static final Logger log = Logger.getLogger(Util.class.getName());
   private static String template;
   private static String slideTemplate;
   private static String englishTemplate;
   private static String chineseTemplate;
   private static byte[] bg;

   static {
      try {
         template = new String(getBytesFromLocal("templates/template.xml"));
         slideTemplate = new String(
               getBytesFromLocal("templates/slideTemplate.xml"));
         englishTemplate = new String(
               getBytesFromLocal("templates/englishTemplate.xml"));
         chineseTemplate = new String(
               getBytesFromLocal("templates/chineseTemplate.xml"));
         bg = getBytesFromLocal("templates/bg.png");
      } catch (AppException e) {
         e.printStackTrace();
      }
   }

   public static byte[] getBytesFromStream(InputStream is) throws AppException {
      DataInputStream dis = new DataInputStream(new BufferedInputStream(is));
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      int d;
      try {
         while (-1 != (d = dis.read())) {
            os.write(d);
         }
      } catch (Exception e) {
         throw new AppException(e.getMessage());
      }
      return os.toByteArray();
   }

   public static byte[] getBytesFromURL(URL url) throws AppException {
      InputStream is;
      try {
         is = url.openStream();
      } catch (Exception e) {
         throw new AppException(e.getMessage());
      }
      return getBytesFromStream(is);
   }

   public static byte[] getBytesFromURL(String url) throws AppException {
      try {
         return getBytesFromURL(new URL(url));
      } catch (Exception e) {
         throw new AppException(e.getMessage());
      }
   }

   public static byte[] getBytesFromLocal(String path) throws AppException {
      try {
         InputStream is = new FileInputStream(path);
         return getBytesFromStream(is);
      } catch (FileNotFoundException e) {
         throw new AppException(e.getMessage());
      }
   }

   public static String insert(String s1, byte[] data, int pos)
         throws UnsupportedEncodingException {
      return s1.substring(0, pos) + Base64.encode(data) + s1.substring(pos);
   }

   public static String insert(String s1, String s2, int pos) {
      return s1.substring(0, pos) + s2 + s1.substring(pos);
   }

   public static String getXMLTemplate() {
      return template;
   }

   public static String getXMLSlideTemplate() {
      return slideTemplate;
   }
   
   public static String getEnglishTemplate() {
      return englishTemplate;
   }

   public static String getChineseTemplate() {
      return chineseTemplate;
   }

   public static byte[] getBg() {
      return bg;
   }

   public static StringTokenizer getTok(String pStr) {
      StringTokenizer tok = new StringTokenizer(pStr, DIVIDER);
      return tok;
   }
   
   public static boolean isEmpty(String str) {
      return null==str || 0==str.trim().length();
   }
   
   public static void tag(String tagName, Long songId) {
      SongProject song = new SongProject(PMF.getPM()).get(songId);
      tag(tagName, song);
      song.closeConnection();
   }
   
   public static void tag(String tagName, SongProject song) {
      Tag tag = Tag.getTagByName(song.getPm(), tagName);
      if (null == tag) {
         tag = new Tag(song.getPm(), tagName);
      }
      
      if(tag.addSong(song.getId())) {
         tag.save();
         song.addTag(tagName);
      }
   }
}
